

import MannageService.Manage;
import MannageService.ManageDaoImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name="Mmessage",value ="/Mmessage")
public class Mmessage extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(true);
        session.setMaxInactiveInterval(60 * 60);//session储存时间为1小时
        //User manage = (User) session.getAttribute("manage");//方便获取登录之后的那个用户的用户名和密码
        Manage manage=(Manage)session.getAttribute("manage");

        String  submitButton= req.getParameter("submitButton");//提交按钮
        String returnButton=req.getParameter("returnButton");//返回按钮
        System.out.println("提交按钮");
        System.out.println("修改按钮");
        String error="  ";

        if (submitButton!=null){
            String password = req.getParameter("password");//密码
            System.out.println("输入的密码"+password);
            System.out.println(manage.getPassword());
            String newpassword = req.getParameter("newpassword");//新密码
            String newphone= req.getParameter("phone");//新手机号
            String newstorename= req.getParameter("storename");//新店铺名

            if (password.equals(manage.getPassword())){
                //UserDaoImp u=new UserDaoImp();//判断用户输入的密码是否跟原密码一致，省去判断空的步骤
                ManageDaoImp m1=new ManageDaoImp();
                System.out.println("111");
                m1.updateUser(manage,newpassword,newphone,newstorename);
                System.out.println("222");


                Manage manage1=m1.finduser(manage.getMname());//获取更改密码后的新用户
                System.out.println("333");
                //session.removeAttribute("manage");//移除原有session名字方便进行存储
                session.setAttribute("manage",manage1);//重新添加session用户
                System.out.println("444");
                error="  修改成功！  ";
                req.setAttribute("error", error);
                req.getRequestDispatcher("mercahntmessage.jsp").forward(req, resp);}



            else {//不相等直接error输出原密码错误并重新定向
                error="  原密码错误！ ";
                req.setAttribute("error", error);
                req.getRequestDispatcher("mercahntmessage.jsp").forward(req, resp);
            }

        }else if(returnButton!=null){//用户按下返回按钮，返回主页面
            System.out.println("return");
            // req.getRequestDispatcher("manageSite.jsp").forward(req, resp);
            resp.sendRedirect("merchantSite.jsp");
        }
    }
}
